package com.todo.strategy.online.application.service.pipeline.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.constants.BizConstants;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.component.redis.RedisService;
import com.todo.strategy.common.constant.StrategyOnlineKeyBuild;
import com.todo.strategy.common.enums.PlanRelationSceneEnum;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.PlanRelationBO;
import com.todo.strategy.online.application.service.pipeline.PlanRelationPipelineService;
import com.todo.strategy.online.infrastructure.entity.po.PlanRelationPO;
import com.todo.strategy.online.infrastructure.repository.PlanRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/3/1 17:27
 * @description
 */
@Slf4j
@Service
public class PlanRelationPipelineServiceImpl implements PlanRelationPipelineService {

    @Resource
    private PlanRelationRepository planRelationRepository;
    @Resource
    private RedisService redisService;

    @Override
    public Boolean insertPlanRelation(String originData) {
        if (originData == null) {
            return false;
        }
        PlanRelationPO planRelationPO = JSONObject.parseObject(originData, PlanRelationPO.class);
        //取第一个作为数据基准
        Long businessId = planRelationPO.getBusinessId();
        Integer businessScene = planRelationPO.getBusinessScene();
        List<PlanRelationPO> alreadyPlanRelationPOList = planRelationRepository.queryPlanRelationListByBizId(businessId, businessScene);
        if (CollectionUtils.isNotEmpty(alreadyPlanRelationPOList)) {
            int sunCount = 1 + alreadyPlanRelationPOList.size();
            if (sunCount > BizConstants.DEFAULT_MAX_PLAN_RELATION_SIZE) {
                log.warn("PlanRelationPipelineService#insertPlanRelation 新增计划关联信息失败，当前业务关联下数据超限，默认不允许操作 businessId:[{}],businessScene:[{}],当前数量:[{}]，新增后数量:[{}]", businessId, businessScene, alreadyPlanRelationPOList.size(), sunCount);
                throw new BizException(BizErrorCodeEnum.RELATION_DATA_EXCESSIVE);
            }
        }

        Boolean inserted = planRelationRepository.insertPlanRelationPO(planRelationPO);
        if (inserted) {
            //缓存操作
            String key = StrategyOnlineKeyBuild.planRelationListKey(PlanRelationSceneEnum.getByType(businessScene), businessId);
            redisService.hSet(key, String.valueOf(planRelationPO.getId()), JSONObject.toJSONString(planRelationPO));
            for (PlanRelationPO item : alreadyPlanRelationPOList) {
                redisService.hSet(key, String.valueOf(item.getId()), JSONObject.toJSONString(item));
            }
        }
        return inserted;
    }

    @Override
    public Boolean updatePlanRelation(Long businessId, String originData) {
        if (originData == null || businessId == null) {
            return false;
        }
        PlanRelationPO already = planRelationRepository.queryPlanRelationPOById(businessId);
        if (already == null) {
            return false;
        }
        PlanRelationPO planRelationPO = JSONObject.parseObject(originData, PlanRelationPO.class);
        Boolean updated = planRelationRepository.updatePlanRelationPOById(planRelationPO);
        if (updated) {
            PlanRelationBO planRelationBO = StrategyConvert.convertToPlanRelationBO(planRelationPO);
            String key = StrategyOnlineKeyBuild.planRelationListKey(planRelationBO.getBusinessScene(), planRelationBO.getBusinessId());
            redisService.hDel(key, String.valueOf(already.getId()));
            redisService.hSet(key, String.valueOf(planRelationPO.getId()), JSONObject.toJSONString(StrategyConvert.convertToPlanRelationBO(planRelationPO)));
        }
        return updated;
    }

    @Override
    public Boolean removePlanRelation(Long businessId) {
        if (businessId == null) {
            return false;
        }
        PlanRelationPO already = planRelationRepository.queryPlanRelationPOById(businessId);
        if (already == null) {
            return true;
        }
        Boolean removed = planRelationRepository.removePlanRelationPOById(businessId);
        if (removed) {
            String key = StrategyOnlineKeyBuild.planRelationListKey(PlanRelationSceneEnum.getByType(already.getBusinessScene()), already.getBusinessId());
            redisService.hDel(key, String.valueOf(already.getId()));
        }
        return removed;
    }
}
